/*
 * @Author: your name
 * @Date: 2020-09-18 11:36:06
 * @LastEditTime: 2021-01-17 21:58:10
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-RESTfulAPI\app\controller\virtualEtc.js
 */
/*
 * @Author: your name
 * @Date: 2020-09-18 11:36:06
 * @LastEditTime: 2020-09-18 15:18:21
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-RESTfulAPI\app\controller\virtualEtc.js
 */
const Controller = require('egg').Controller
let moment = require('moment');
class ETCController extends Controller {
    constructor(ctx) {
        super(ctx)

        this.addRule = {
            sceneName: { type: 'string', required: true, allowEmpty: false },
            planId: { type: 'string', required: true, allowEmpty: false },
            dateBegin: { type: 'string', required: true, allowEmpty: false },

            dateEnd: { type: 'string', required: true, allowEmpty: false },
            carNumber: { type: 'number', required: true, allowEmpty: false },
            ODCities: { type: 'string', required: true, allowEmpty: false },
            runTime: { type: 'string', required: true, allowEmpty: false },
        }

        this.listRule = {
            current: { type: 'int', required: true, allowEmpty: false },
            size: { type: 'int', required: true, allowEmpty: false }
        }
        this.updateRule = {
            _id: { type: 'string', required: true, allowEmpty: false },
            sceneName: { type: 'string', required: true, allowEmpty: false },
            dateBegin: { type: 'string', required: true, allowEmpty: false },
            dateEnd: { type: 'string', required: true, allowEmpty: false },
            carNumber: { type: 'number', required: true, allowEmpty: false },
            ODCities: { type: 'string', required: true, allowEmpty: false },
            runTime: { type: 'string', required: true, allowEmpty: false },
        }
        this.removeRule = {
            ids: { type: 'string', required: true, allowEmpty: false },
        }
        this.detailRule = {
            id: { type: 'string', required: true, allowEmpty: false },
        }

    }

    async add() { //需要有拷贝操作
        const { ctx } = this
        ctx.validate(this.addRule)
        let { sceneName, planId, dateBegin, dateEnd, carNumber, ODCities, runTime } = ctx.request.body
        let selectPlan = await this.ctx.model.PlanManagerModel.findOne({ '_id': planId })
        if (!selectPlan) {
            ctx.helper.InvaidRequest({ ctx, msg: "planId 异常" })
            return
        }
        let newScene = await this.ctx.model.Scene.create({ sceneName, planId, dateBegin, dateEnd, carNumber, ODCities, runTime })

        let PlanEtcStationModel = await this.ctx.model.PlanEtcStationModel.find({ planId: planId })
        PlanEtcStationModel = JSON.parse(JSON.stringify(PlanEtcStationModel))
        PlanEtcStationModel = PlanEtcStationModel.map((item, index) => {
            item['sceneId'] = newScene.id
            delete item['_id']
            return item
        })
        await this.ctx.model.SceneEtcStationModel.insertMany(PlanEtcStationModel)
        ctx.helper.success({ ctx, res: {} })
    }

    async list() {
        const { ctx } = this
        ctx.validate(this.listRule)
        let { current, size } = ctx.request.body
        current = parseInt(current) - 1
        size = parseInt(size)
        let totalCount = await this.ctx.model.Scene.find({}).count()
        let managerSces = await this.ctx.model.Scene.find({}).populate({ path: 'planId', select: 'mapId' }).sort({ createdAt: 1 }).skip(current * size).limit(size)
        managerSces = JSON.parse(JSON.stringify(managerSces))
        managerSces = managerSces.map((item, index) => {
            item['mapId'] = item.planId.mapId
            item['planId'] = item.planId._id
            return item
        })
        ctx.helper.success({ ctx, res: { records: managerSces, total: totalCount } })
    }
    async update() {
        const { ctx } = this
        ctx.validate(this.updateRule)
        let { _id, sceneName, planId, dateBegin, dateEnd, carNumber, ODCities, runTime } = ctx.request.body

        let plan = await this.ctx.model.Scene.findOneAndUpdate( //4.删除用户上的设备信息
            {
                '_id': _id
            },
            {
                sceneName, planId, dateBegin, dateEnd, carNumber, ODCities, runTime
            },
            { upsert: true, new: true }
        )
        ctx.helper.success({ ctx, res: plan })
    }

    async remove() {
        const { ctx } = this
        ctx.validate(this.removeRule)
        let { ids } = ctx.request.body
        ids = ids.split(',')

        await this.ctx.model.Scene.remove({ _id: { $in: ids } })
        await this.ctx.model.SceneEtcStation.remove({ sceneId: { $in: ids } })

        //await this.ctx.model.Scene.remove({ _id: { $in: ids } })


        ctx.helper.success({ ctx, res: {} })
    }

    async detail() {
        const { ctx } = this
        ctx.validate(this.detailRule)
        let { id } = ctx.request.body
        let result = await this.ctx.model.Scene.findOne({ _id: id })
        ctx.helper.success({ ctx, res: result })
    }






}


module.exports = ETCController